﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Mail;
using SQLServerOptimizer.PL.WPF.Datasets.IssuesReportDatasetTableAdapters;
using System.Configuration;
using SQLServerOptimizer.PL.WPF.Datasets;
using Microsoft.Reporting.WinForms;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace SQLServerOptimizer.PL.WPF
{
    public class MailReport
    {

        
        public void mail(string from, string to, string username, string password, string host, string port, string filename)
        {
            getreport(filename);
            try{
            MailMessage mail = new MailMessage();

            mail.From = new System.Net.Mail.MailAddress(from);
                mail.Attachments.Add(new Attachment(filename+".pdf"));
            // The important part -- configuring the SMTP client
            SmtpClient smtp = new SmtpClient();
            mail.To.Add(new MailAddress(to));

            smtp.EnableSsl = true;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network; // [2] Added this
            smtp.UseDefaultCredentials = false; // [3] Changed this

            smtp.Credentials = new NetworkCredential(username, password);


            smtp.Host = host;

            //recipient address



            smtp.Port = Convert.ToInt32(port);

            //Formatted mail body
            mail.IsBodyHtml = true;

            mail.Subject = "SQLServerOptimizer report";
            mail.Body = "The issue report is added as attachment";
            smtp.Send(mail);

 }
            catch (Exception mailexept){
              
            }        
        }

        public void getreport(string filename)
        {

            ReportViewer reportViewer = new ReportViewer();
            
            IssuesTableAdapter adapter = new IssuesTableAdapter();
            adapter.Connection.ConnectionString = ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"];

            IssuesReportDataset dataset = new IssuesReportDataset();

            adapter.Fill(dataset.Issues, 3, 2, -1, -1,-1);


            BindingSource bindingSource = new BindingSource();

            bindingSource.DataSource = dataset;
            bindingSource.DataMember = "Issues";


            ReportDataSource reportDataSource = new ReportDataSource();
       
            reportDataSource.Name = "Issues";
            reportDataSource.Value = bindingSource;

            ReportParameter param = new ReportParameter("ExpandLevel", "2");

            reportViewer.LocalReport.ReportPath = System.Windows.Forms.Application.StartupPath + @"\Reports\Issues.rdlc";
            reportViewer.LocalReport.DataSources.Add(reportDataSource);
            reportViewer.LocalReport.EnableHyperlinks = true;
            reportViewer.BorderStyle = BorderStyle.None;
            reportViewer.LocalReport.SetParameters(param);
            Warning[] warnings;
            string[] streamids;
            string mimeType;
            string encoding;
            string filenameExtension;

            byte[] bytes = reportViewer.LocalReport.Render(
                "PDF", null, out mimeType, out encoding, out filenameExtension,
                out streamids, out warnings);

            using (FileStream fs = new FileStream(filename+".pdf", FileMode.Create))
            {
                fs.Write(bytes, 0, bytes.Length);
                fs.Close();
            }

           
        }





    }
}
